/*
 *      logviewer.h
 *
 *      Copyright 2009 David Vachulka <david@konstrukce-cad.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef LOGVIEWER_H
#define LOGVIEWER_H

#include "defs.h"
#include "fxext.h"
#ifndef DXTEXT_H
#include "dxtext.h"
#endif

class LogViewer;

class LogItem : public FXTreeItem
{
    FXDECLARE(LogItem)
    friend class LogViewer;
protected:
    FXTime date; // Time of item
private:
    LogItem(const LogItem&);
    LogItem & operator=(const LogItem&);
protected:

    LogItem() : date(0){}
public:
    enum
    {
        ROOT = 512, /// Root Item
        NETWORK = 1024, /// Network item
        CHANNEL = 2048, /// Channel/Query item
        YEAR = 4096, /// Year item
        MONTH = 8192, /// Month item
        LOGFILE = 16384 /// LogFile item
    };
public:
    /// Constructor
    LogItem(const FXString& text, FXIcon* oi = NULL, FXIcon* ci = NULL, void* ptr = NULL) : FXTreeItem(text, oi, ci, ptr), date(0)
    {
        state = HASITEMS;
    }
    /// Return TRUE if this is a LogFile item
    FXbool isLogFile() const
    {
        return (state & LOGFILE) != 0;
    }
    /// Return TRUE if this is a Month item
    FXbool isMonth() const
    {
        return (state & MONTH) != 0;
    }
    /// Return TRUE if this is Year item
    FXbool isYear() const
    {
        return (state & YEAR) != 0;
    }
    /// Return TRUE if this is Channel/Query item
    FXbool isChannel() const
    {
        return (state & CHANNEL) != 0;
    }
    /// Return TRUE if this is Network item
    FXbool isNetwork() const
    {
        return (state & NETWORK) != 0;
    }
    /// Return TRUE if this is Root item
    FXbool isRoot() const
    {
        return (state & ROOT) != 0;
    }
    /// Return the date for this item
    FXTime getDate() const
    {
        return date;
    }
};

class LogViewer: public FXTopWindow
{
    FXDECLARE(LogViewer)
public:
    LogViewer(FXApp *app, const FXString &lpath, FXFont *fnt);
    virtual ~LogViewer();

    virtual void show();
    virtual void show(FXuint placement);

    long onClose(FXObject*,FXSelector,void*);
    long onKeyPress(FXObject*,FXSelector,void*);
    long onTree(FXObject*,FXSelector,void*);
    long onRightTree(FXObject*,FXSelector,void*);
    long onSearch(FXObject*,FXSelector,void*);
    long onSearchNext(FXObject*,FXSelector,void*);
    long onReset(FXObject*,FXSelector,void*);
    long onCmdSearchOptions(FXObject*,FXSelector,void*);
    long onPack(FXObject*,FXSelector,void*);
    long onUnpack(FXObject*,FXSelector,void*);
    long onDelete(FXObject*,FXSelector,void*);
    long onTextLink(FXObject*,FXSelector,void*);
    long onTextRightMouse(FXObject*,FXSelector,void*);
    long onTextMiddleMouse(FXObject*,FXSelector,void*);
    long onOpenLink(FXObject*,FXSelector,void*);
    long onCopyLink(FXObject*,FXSelector,void*);
    long onClipboardRequest(FXObject*,FXSelector,void*);
    long onStatusTimeout(FXObject*,FXSelector,void*);
    void setFont(FXFont*);
    void setLogPath(const FXString &path);

private:
    LogViewer() {}
    LogViewer(const LogViewer&);

    FXbool loadFile(const FXString& file);
    void loadTree();
    FXString getItemPathname(const LogItem* item);
    void listChildItems(LogItem *par);
    void enableAllItems();
    void scan();
    FXint getNickStyle(const FXString &nick);
    void clearLog();
    void appendLogLine(const FXString &line);
    void appendLinkText(const FXString &txt, FXint stylenum);
    FXint hiliteStyleExist(FXColor foreColor, FXColor backColor, FXuint style);
    void createHiliteStyle(FXColor foreColor, FXColor backColor, FXuint style);
    LogItem *getChannelItem(LogItem *item);
    LogItem *getNetworkItem(LogItem *item);
    void searchInLogFile();
    FXint searchInChannel(LogItem *item, FXbool showMessage=TRUE);
    FXint searchInNetwork(LogItem *item, FXbool showMessage=TRUE);
    void searchInAll();
    void listChannelChildItems(LogItem *channel);
    void listNetworkChildItems(LogItem *network);
    void listRootChildItems(LogItem *root);
    LogItem* getItem(LogItem *parent, FXString text);
    dxStringArray getLogFiles(LogItem *parent);
    void updateStatus(const FXString &text);
    FXString getLogFileName(const LogItem* item);

    FXVerticalFrame *m_listframe, *m_textframe, *m_treeframe;
    FXHorizontalFrame *m_content, *m_buttonframe, *m_searchframe, *m_statusbar;
    FXSplitter *m_splitter;
    FXTreeList *m_treeHistory;
    FXTextField *m_searchfield;
    dxEXButton *m_buttonClose, *m_buttonSearch, *m_buttonReset;
    FXGroupBox *m_group;
    FXRadioButton *m_buttonAll, *m_buttonNetwork, *m_buttonChannel, *m_buttonFile;
    FXCheckButton *m_buttonIcase;
    dxText *m_text;
    FXLabel *m_statuslabel;
    LogItem *m_itemOnRight;
    FXString m_logPath, m_searchstring;
    FXDataTarget m_targetAll, m_targetNetwork, m_targetChannel, m_targetFile, m_targetIcase;
    FXbool m_all, m_network, m_channel, m_file, m_icase, m_treeLoaded;
    dxHiliteArray m_textStyleList;
    FXString m_link;
};

#endif  /* LOGVIEWER_H */

